package com.ruoyi.web.controller.system;  

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AdministrativeAreaResult;
import com.ruoyi.system.domain.CasesNumberProcessadmin;
import com.ruoyi.system.domain.CasesNumberProcessadminRe;
import com.ruoyi.system.service.ICivilAreaCasesNumberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AdministrativeAreaCasesNumber;
import com.ruoyi.system.service.IAdministrativeAreaCasesNumberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 行政公益诉讼领域情况Controller
 * 
 * @author ruoyi
 * @date 2025-05-23
 */
@RestController
@RequestMapping("/system/number")
public class AdministrativeAreaCasesNumberController extends BaseController
{
    @Autowired
    private IAdministrativeAreaCasesNumberService administrativeAreaCasesNumberService;

    @Autowired
    private ICivilAreaCasesNumberService civilAreaCasesNumberService;

    private static final List<String> REGION_ORDER = Arrays.asList(
            "本院","哈尔滨市院", "齐齐哈尔市院", "牡丹江市院","佳木斯市院","大庆市院", "鸡西市院", "双鸭山市院","伊春市院",
            "七台河市院", "鹤岗市院",   "黑河市院","绥化市院", "大兴安岭分院", "农垦分院","林区分院","哈尔滨铁路分院", "黑龙江省院"
    );



    /**
     * 查询行政公益诉讼领域情况列表
     */
    @PreAuthorize("@ss.hasPermi('system:number:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdministrativeAreaCasesNumber administrativeAreaCasesNumber)
    {
        startPage();
        List<AdministrativeAreaCasesNumber> list = administrativeAreaCasesNumberService.selectAdministrativeAreaCasesNumberList(administrativeAreaCasesNumber);
        return getDataTable(list);
    }

    private static void checkDate(AdministrativeAreaCasesNumber administrativeAreaCasesNumber) throws Exception {
        Object startTime = administrativeAreaCasesNumber.getParams().get("beginTime");
        Object endTime = administrativeAreaCasesNumber.getParams().get("endTime");
        if (null==startTime) {
            throw new Exception("开始时间不能为空");
        } else {
            administrativeAreaCasesNumber.setStartTime(startTime.toString().substring(0,7));
            administrativeAreaCasesNumber.setStartTimeLast(StringUtils.strdif(administrativeAreaCasesNumber.getStartTime()));
        }
        if (null==endTime) {
            throw new Exception("结束时间不能为空");
        } else {
            administrativeAreaCasesNumber.setEndTime(endTime.toString().substring(0,7) );
            administrativeAreaCasesNumber.setEndTimeLast(StringUtils.strdif(administrativeAreaCasesNumber.getEndTime()));
        }
    }
    @PreAuthorize("@ss.hasPermi('system:processadmin:list')")
    @GetMapping("/listRe")
    public TableDataInfo listRe(AdministrativeAreaCasesNumber administrativeAreaCasesNumber) throws Exception {
        startPage();
        checkDate(administrativeAreaCasesNumber);
        List<AdministrativeAreaResult> list = administrativeAreaCasesNumberService.selectAdministrativeAreaResult(administrativeAreaCasesNumber);
        List<AdministrativeAreaResult> listcivi = civilAreaCasesNumberService.selectResultAreaCasesNumberList(administrativeAreaCasesNumber);
        // Long total=0L;
        //Long totalLast=0L;
        long i=1;
        for (AdministrativeAreaResult re : list) {
            for (AdministrativeAreaResult recivi : listcivi)
                if (StringUtils.isNotEmpty(re.getDepartmentName()) && re.getDepartmentName().equals(recivi.getDepartmentName())) {
                    Long caseparxzTotal=re.getCaseparxzTotal();
                    Long caseparxzTotallast=re.getCaseparxzTotallast();
                    Long caseparmsTotal=recivi.getCaseparmsTotal();
                    Long caseparmsTotallast=recivi.getCaseparmsTotallast();
                    long totollast=caseparxzTotallast+caseparmsTotallast;
                    long totol=caseparxzTotal+caseparmsTotal;
                    Long totolAl=re.getCaseparxzAlTotal()+recivi.getCaseparmsAlTotal();
                    Long totolAllast=re.getCaseparxzAlTotallast()+recivi.getCaseparmsAlTotallast();
                    re.setCaseparTotal(totol);
                    re.setPeryear(StringUtils.strPercent(totol-totollast,totollast));
                    String num1str=StringUtils.strPercent(totol, totolAl);
                    String num2str=StringUtils.strPercent(totollast, totolAllast);
                    re.setPerZhan(StringUtils.strPercent(totol, totolAl));
                    BigDecimal num1=new BigDecimal(0.00);
                    BigDecimal num2=new BigDecimal(0.00);
                    if (num1str.contains("%")) {
                        num1 = new BigDecimal(num1str.replace("%", ""));
                    }
                    if (num2str.contains("%")) {
                        num2 = new BigDecimal(num2str.replace("%", ""));
                    }
                    re.setPeryearZhan(num1.subtract(num2)+"%");
                    re.setOrNuber(Long.parseLong(String.valueOf(REGION_ORDER.indexOf(re.getDepartmentName())+1)));
                }
        }
        list.sort(Comparator.comparingInt(
                vo -> REGION_ORDER.indexOf(vo.getDepartmentName())
        ));
        return getDataTable(list);
    }


    /**
     * 导出行政公益诉讼领域情况列表
     */
    @PreAuthorize("@ss.hasPermi('system:number:export')")
    @Log(title = "行政公益诉讼领域情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdministrativeAreaCasesNumber administrativeAreaCasesNumber)
    {
        List<AdministrativeAreaCasesNumber> list = administrativeAreaCasesNumberService.selectAdministrativeAreaCasesNumberList(administrativeAreaCasesNumber);
        ExcelUtil<AdministrativeAreaCasesNumber> util = new ExcelUtil<AdministrativeAreaCasesNumber>(AdministrativeAreaCasesNumber.class);
        util.exportExcel(response, list, "行政公益诉讼领域情况数据");
    }

    /**
     * 获取行政公益诉讼领域情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:number:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(administrativeAreaCasesNumberService.selectAdministrativeAreaCasesNumberById(id));
    }

    /**
     * 新增行政公益诉讼领域情况
     */
    @PreAuthorize("@ss.hasPermi('system:number:add')")
    @Log(title = "行政公益诉讼领域情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdministrativeAreaCasesNumber administrativeAreaCasesNumber)
    {
        return toAjax(administrativeAreaCasesNumberService.insertAdministrativeAreaCasesNumber(administrativeAreaCasesNumber));
    }

    /**
     * 修改行政公益诉讼领域情况
     */
    @PreAuthorize("@ss.hasPermi('system:number:edit')")
    @Log(title = "行政公益诉讼领域情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdministrativeAreaCasesNumber administrativeAreaCasesNumber)
    {
        return toAjax(administrativeAreaCasesNumberService.updateAdministrativeAreaCasesNumber(administrativeAreaCasesNumber));
    }

    /**
     * 删除行政公益诉讼领域情况
     */
    @PreAuthorize("@ss.hasPermi('system:number:remove')")
    @Log(title = "行政公益诉讼领域情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(administrativeAreaCasesNumberService.deleteAdministrativeAreaCasesNumberByIds(ids));
    }
}
